package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    /**
     * 查询评论列表
     * 接口路径：GET
     * 参数Query：
     *  movementId，page,pagesize
     * 返回值：
     *  PageResult  --- List<CommentVo>
     */
    @GetMapping
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10")  Integer pagesize,
                                   String movementId) {
        PageResult vo = commentsService.findComments(movementId,page,pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 发布评论
     * 接口路径：POST
     * 参数Body：
     *   movementId，comment（评论内容）
     * 返回值：
     *   void
     */
    @PostMapping
    public ResponseEntity saveComment(@RequestBody Map map) {
        //获取参数
        String movementId = map.get("movementId").toString();
        String comment = map.get("comment").toString();
        //调用service
        commentsService.saveComment(movementId,comment);
        //返回
        return ResponseEntity.ok(null);
    }
}
